package com.theo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theo.entity.UserInfo;
import com.theo.mapper.UserInfoMapper;
import com.theo.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo findByAccount(UserInfo user) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name", user.getLoginName());
        wrapper.eq("password", user.getPassword());
        return userInfoMapper.selectOne(wrapper);
    }

    @Override
    public List<UserInfo> queryUserList() {
        return userInfoMapper.selectList(null);
    }

    @Override
    public String validateLoginName(String loginName) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name", loginName);
        UserInfo user = userInfoMapper.selectOne(wrapper);
        System.out.println(user);
        return user == null ? "" : "您输入的用户名已存在";
    }

    @Override
    public UserInfo queryUserById(Integer id) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        return userInfoMapper.selectOne(wrapper);
    }

    @Override
    public List<UserInfo> queryUserByLoginNameAndEmail(String loginName, String email) {
        return null;
    }

    @Override
    public int addUser(UserInfo user) {
        return userInfoMapper.insert(user);
    }

    @Override
    public int updateUser(UserInfo user) {
        return userInfoMapper.updateById(user);
    }

    @Override
    public int deleteUser(Integer id) {
        return userInfoMapper.deleteById(id);
    }
}
